package com.zhihuiyun.library.switchbutton;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

import com.zhihuiyun.library.R;


/**
 * Created by yan
 * on 2018/2/11
 */

public class SwitchButton extends RelativeLayout {

    public static final int OPEN = 1;   //状态开
    public static final int CLOSE = 0;   //状态关

    private int defaultStatus;   //默认状态

    private int currentStatus;     //当前状态
    private OnSwitchListener onSwitchListener;   //状态变化接口

    private TypedArray typedArray;  //自定义属性数组

    private RelativeLayout relativeLayout;   //相对布局，代表按钮的下面一部分
    private View view;  //view，代表上方的圆形部分

    public SwitchButton(Context context) {
        super(context);
    }

    public SwitchButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_switch, this);   //实现布局
        typedArray = context.obtainStyledAttributes(attrs, R.styleable.SwitchButton);   //获取自定义属性
        initView();   //初始化视图和控件
        init();       //初始化按钮的状态
    }

    private void initView() {
        relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout_bg);

        view = findViewById(R.id.view_scroll);
    }

    private void init() {
        defaultStatus = typedArray.getInt(R.styleable.SwitchButton_defaultStatus, 1);
        if (defaultStatus == 0) {
            setViewLocation(view, RelativeLayout.ALIGN_PARENT_LEFT);
            currentStatus = CLOSE;
        } else if (defaultStatus == 1) {
            setViewLocation(view, RelativeLayout.ALIGN_PARENT_RIGHT);  //设置圆形部分的位置
            currentStatus = OPEN;
        }
    }

    private void setViewLocation(View view, int location) {
        LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();  //获取view的布局参数
        /**
         *  将当前需要设置的布局的相反设置从参数设置中去除
         *  在开启状态的时候，圆形部分在最右侧，将按钮切换为关闭状态的时候
         *  如果不把view在父布局最右侧这一属性去掉，会导致ALIGN_PARENT_LEF
         *  T和ALIGN_PARENT_RIGHT属性同时设置
         *  导致布局出现错误
         */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (location == RelativeLayout.ALIGN_PARENT_LEFT) {
                relativeLayout.setBackgroundResource(R.drawable.bg_switch_bottom);
                layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            } else if (location == RelativeLayout.ALIGN_PARENT_RIGHT) {
                relativeLayout.setBackgroundResource(R.drawable.bg_switch_bottom_open);
                layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
                layoutParams.addRule(location);
            }

        } else {
            layoutParams.addRule(location, 0);
        }
        view.setLayoutParams(layoutParams);
    }

    /**
     * 关闭按钮
     */
    private void closeButton() {
        setViewLocation(view, RelativeLayout.ALIGN_PARENT_LEFT);
        currentStatus = CLOSE;
    }

    /**
     * 打开按钮
     */
    private void openButton() {
        setViewLocation(view, RelativeLayout.ALIGN_PARENT_RIGHT);
        currentStatus = OPEN;
    }

    public void setCheck(boolean isOpen) {
        if (isOpen)
            openButton();
        else
            closeButton();
    }

    public void setOnSwitchListener(OnSwitchListener onSwitchListener) {
        this.onSwitchListener = onSwitchListener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                /**
                 * 改变状态
                 */
                changeStatus();
                break;
        }
        return super.onTouchEvent(event);
    }

    private void changeStatus() {
        if (currentStatus == OPEN) {
            relativeLayout.setBackgroundResource(R.drawable.bg_switch_bottom);
            closeButton();
        } else if (currentStatus == CLOSE) {
            relativeLayout.setBackgroundResource(R.drawable.bg_switch_bottom_open);
            openButton();
        }
        if (onSwitchListener != null) {
            onSwitchListener.onSwitchChange(currentStatus);  //调用监听改变时候处理逻辑的函数
        }
    }
}
